package com.wsz.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.commonutils.R;
import com.wsz.staservice.client.UcenterClient;
import com.wsz.staservice.pojo.StatisticsDaily;
import com.wsz.staservice.service.StatisticsDailyService;
import com.wsz.staservice.mapper.StatisticsDailyMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wangs
* @description 针对表【statistics_daily(网站统计日数据)】的数据库操作Service实现
* @createDate 2022-10-06 19:23:13
*/
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily>
    implements StatisticsDailyService{

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        R r = ucenterClient.countRegister(day);
        Integer countRegister =(Integer) r.getData().get("countRegister");

        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countRegister);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select(type,"date_calculated");

        List<StatisticsDaily> staList = baseMapper.selectList(queryWrapper);

        //前端数组json后端list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numdateList = new ArrayList<>();

        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            date_calculatedList.add(daily.getDateCalculated());
            switch (type){
                case "login_num":
                    numdateList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numdateList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numdateList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numdateList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numdateList", numdateList);
        return map;
    }
}




